package com.andnnl.pool.handle;

import com.andnnl.pool.Msg;
import com.andnnl.pool.TaskManage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 默认整个列表有问题全部重试
 * Created by chenss on 2022/5/31.
 */
public abstract class RetryListHandle<T> extends AbsMsgHandle<T> {
    private static Logger logger = LoggerFactory.getLogger(RetryListHandle.class);

    @Override
    public void handleMsg(TaskManage<T> taskManage, List<Msg<T>> msgs) {
        List<T> list = msgs.stream().map(m -> m.getData()).collect(Collectors.toList());
        try {
            handleMsg(list);
        } catch (Exception e) {
            logger.info("catch Exception:" + e.getMessage() + " list.size:" + list.size(), e);
            for (Msg<T> msg : msgs) {
                boolean isAdd = taskManage.addRetryMsg(msg);//没配置重试机制报IllegalArgumentException
                logger.error("retryTimes " + msg.getRetryTimes() + ",key:" + msg.getUserName() + ",isAdd:" + isAdd);
            }
        }
    }

    protected abstract void handleMsg(List<T> list);

}
